//
//  ShoppingCartCell.m
//  FKGW
//
//  Created by song ce on 2017/2/22.
//  Copyright © 2017年 song ce. All rights reserved.
//

//测试商品1的描述信息

#import "ShoppingCartCell.h"
#import "UIView+Extend.h"
#import "OSSHelper.h"
#define kMainWindow  [UIApplication sharedApplication].keyWindow
@implementation ShoppingCartCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.count=1;
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.count];
    self.selectionStyle= UITableViewCellSelectionStyleNone;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.selectedView addGestureRecognizer:tap];

    //添加左滑手势
    UISwipeGestureRecognizer *swipLeft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
    swipLeft.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.moveView addGestureRecognizer:swipLeft];
    //添加右滑手势
    UISwipeGestureRecognizer *swipRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
    swipRight.direction=UISwipeGestureRecognizerDirectionRight;
    [self.moveView addGestureRecognizer:swipRight];
//    self.bottonV.alpha = 0;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenTiyanBtn) name:@"hiddenTiyanBtn" object:nil];
    
     self.xOssProcess =  [NSString stringWithFormat:@"image/resize,m_fill,w_%d,h_%d",68,65];
}

-(void)tapAction:(UITapGestureRecognizer*)tap{
    if(self.rowSelectedBlock){
        self.rowSelectedBlock();
    }
}
-(void)hiddenTiyanBtn{
    if(self.isShowingSelectV){
        [self selectedButton:self.yibanButton];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter ]removeObserver:self];
}
- (void)swip:(UISwipeGestureRecognizer *)swipe{
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        // 打开
        [self openMenu];
        // 将其他已打开的关闭
        if( self.mySwipeBlock ){
            self.mySwipeBlock();
        }
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){
        // 关闭
        [self closeMenu];
    }
}

-(void)setModel:(ShopCartModel *)model{
    _model = model;
    [[OSSHelper sharedHelper] OSSDownLoadWithObjectKey:_model.pPic xOssProcess:self.xOssProcess  downLoadBlock:^(NSData *data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.goodsImageView.image = [UIImage imageWithData:data];
        });
    }];
    self.goodsNameLabel.text = _model.name;
    self.priceLabel.text = [NSString stringWithFormat:@"￥ %@",_model.pPrice];
    if([_model.pProperty isKindOfClass:[NSNull class]]){
        self.standardLabel.text = @"";
    }else{
       self.standardLabel.text = _model.pProperty;
    }
    self.count = _model.goodsCount;
    self.countLabel.text = [NSString stringWithFormat:@"%ld",_model.goodsCount];
    
    //设置选中按钮状态
    if(_model.isSelected){
        self.selectedBtn.selected = YES;
        [self.selectedBtn setImage:[UIImage imageNamed:@"icon_xuanzhong"] forState:UIControlStateNormal];
    }else{
        self.selectedBtn.selected = NO;
        [self.selectedBtn setImage:[UIImage imageNamed:@"icon_weixuanzhong"] forState:UIControlStateNormal];
    }
    //设置体验状态
    NSString *status;
    if([_model.pStatus intValue]==0){
        status = @"未体验";
    }
    if([_model.pStatus intValue]==1){
        status = @"喜欢";
    }
    if([_model.pStatus intValue]==2){
        status = @"一般";
    }
    if([_model.pStatus intValue]==3){
        status = @"不喜欢";
    }
    [self.yibanButton setTitle:status forState:UIControlStateNormal];
}

//选择体验状态
- (IBAction)selectedButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    __weak typeof(self) weakSelf = self;
    if(sender.isSelected){
        [[NSNotificationCenter defaultCenter]postNotificationName:@"removePreviousView" object:nil];
        self.isShowingSelectV = YES;
        weakSelf.bottonV.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        [UIView animateWithDuration:.25 animations:^{
            weakSelf.bottonV.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
                  }];
        [weakSelf tiyan:sender];
    }else{
        self.isShowingSelectV = NO;
        [UIView animateWithDuration:.25 animations:^{
            [weakSelf.bottonV removeFromSuperview];
            weakSelf.bottonV =nil;
        }];
    }
}

-(void)tiyan:(UIButton*)sender{
    CGRect absoluteRect = [sender convertRect:sender.bounds toView:kMainWindow];
    CGPoint relyPoint = CGPointMake(absoluteRect.origin.x+12 , absoluteRect.origin.y + absoluteRect.size.height-6);
    self.bottonV.origin =relyPoint;
    [self.window addSubview:self.bottonV];
    __weak typeof(self) weakSelf = self;

    self.bottonV.tiyanBlock= ^(UIButton*sender){
        if(weakSelf.tiyanBlock){
            weakSelf.tiyanBlock(sender.currentTitle);
        }
        [weakSelf.yibanButton setTitle: sender.currentTitle  forState:UIControlStateNormal];
        [weakSelf.yibanButton setTitleColor:sender.currentTitleColor forState:UIControlStateNormal];
        [weakSelf selectedButton:weakSelf.yibanButton];
    };
}

 // 删除
- (IBAction)delete:(UIButton *)sender {
    if(self.myDeleteBlock){
        self.myDeleteBlock();
    }
}

//结算
- (IBAction)jiesuan:(UIButton *)sender {
    // jisuan
    if(self.myJiesunBlock){
        self.myJiesunBlock();
    }
}
//数量减
- (IBAction)jian:(UIButton *)sender {
    if(self.count==1) self.count=1;
    if(self.count>1){
        self.count--;
        self.countLabel.text =[NSString stringWithFormat:@"%d",self.count];
        if(self.mcountBlock){
            self.mcountBlock(self.count);
        }
    }
}
//数量加
- (IBAction)jia:(UIButton *)sender {
    self.count++;
    self.countLabel.text =[NSString stringWithFormat:@"%d",self.count];
    if(self.acountBlock){
        self.acountBlock(self.count);
    }
}

//单元格选中左侧按钮
- (IBAction)cellSeclectedButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    if(sender.selected){
        [sender setImage:[UIImage imageNamed:@"icon_xuanzhong"] forState:UIControlStateNormal];
    }else{
        [sender setImage:[UIImage imageNamed:@"icon_weixuanzhong"] forState:UIControlStateNormal];
    }
    if(self.selectedBlock){
        self.selectedBlock();
    }
}


- (void)openMenu{
    if (self.isOpen) {
        return;
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.moveView.center = CGPointMake(self.moveView.center.x - 140, self.moveView.center.y);
    }completion:^(BOOL finished) {
        if(finished){
            self.isOpen = YES;
        }
    }];
}

/**关闭左滑菜单*/
-(void)closeMenu
{
    if(!_isOpen)
        return;
    [UIView animateWithDuration:0.5 animations:^{
        self.moveView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, self.moveView.center.y);
    }completion:^(BOOL finished) {
        if (finished) {
            self.isOpen = NO;
        }
    }];
    
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if(self.bottonV.alpha==1){
        for(UIView *v2 in self.bottonV.subviews){
            CGPoint buttonPoint = [v2 convertPoint:point fromView:self];
                if ([v2 pointInside:buttonPoint withEvent:event]) {
            return v2;
                }
        }
    }
    return view;
}

-(UIView *)bottonV{
    if(_bottonV==nil){
        _bottonV =[[[NSBundle mainBundle]loadNibNamed:@"CartView" owner:self options:nil]lastObject];
    }
    return _bottonV;
}

@end
